package com.situ.dao;

import com.situ.bean.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CategoryMapper {

    // 获取所有商品种类
    @Select("SELECT * FROM category")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "imgurl", column = "imgurl"),
            @Result(property = "pid", column = "pid"),
            @Result(property = "isdel", column = "isdel"),
            @Result(property = "inserttime", column = "inserttime"),
            @Result(property = "updatetime", column = "updatetime"),
            @Result(property = "parent", column = "pid", javaType = Category.class,
                    one = @One(select = "getCategoryById"))
    })
    List<Category> getAllCategories();

    // 根据ID获取商品种类
    @Select("SELECT * FROM category WHERE id = #{id}")
    Category getCategoryById(int id);

    // 添加商品种类
    @Insert("INSERT INTO category (name, imgurl, pid, isdel, inserttime, updatetime) " +
            "VALUES (#{name}, #{imgurl}, #{pid}, #{isdel}, #{inserttime}, #{updatetime})")
    void addCategory(Category category);

    // 更新商品种类
    @Update("UPDATE category SET name = #{name}, imgurl = #{imgurl}, pid = #{pid}, " +
            "isdel = #{isdel}, updatetime = #{updatetime} WHERE id = #{id}")
    void updateCategory(Category category);

    // 删除商品种类
    @Delete("DELETE FROM category WHERE id = #{id}")
    void deleteCategory(int id);

    // 获取所有父级分类
    @Select("SELECT * FROM category WHERE pid = 0")
    List<Category> getAllParentCategories();

    // 根据父级ID获取子分类
    @Select("SELECT * FROM category WHERE pid = #{pid}")
    List<Category> getCategoriesByParentId(int pid);
}